package com.shuzi.MediaShare.control;

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.MemoryCacheAware;
import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.shuzi.MediaShare.R;
import com.shuzi.MediaShare.view.FadeAnimation;

/**
 * 加载图片管理器
 * 
 * @author lung 下午9:17:44 2013
 */
public class ImageLoaderManager
{

	private static ImageLoaderManager instance = null;

	public ImageLoader imageLoader;

	public DisplayImageOptions options = new DisplayImageOptions.Builder()
			.showStubImage(R.drawable.home_movie)
			.showImageForEmptyUri(R.drawable.home_movie)
			.showImageOnFail(R.drawable.home_movie).cacheInMemory()
			.cacheOnDisc().displayer(new FadeAnimation(350)).build();

	/**
	 * 构造方法
	 */
	public ImageLoaderManager()
	{
		imageLoader = ImageLoader.getInstance();
	}

	/**
	 * 获取图片管理器实例
	 * 
	 * @return
	 */
	public static ImageLoaderManager getInstace()
	{
		if (instance == null)
		{
			instance = new ImageLoaderManager();
		}
		return instance;
	}

	/**
	 * 初始化
	 */
	public void init(Context context)
	{
		int memoryCacheSize = (int) (Runtime.getRuntime().maxMemory() / 8);

		MemoryCacheAware<String, Bitmap> memoryCache;

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)
		{
			memoryCache = new LruMemoryCache(memoryCacheSize);
		} else
		{
			memoryCache = new LRULimitedMemoryCache(memoryCacheSize);
		}

		File cacheDir = StorageUtils.getCacheDirectory(context);
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context).threadPriority(Thread.NORM_PRIORITY - 2)
				.memoryCache(memoryCache)
				.discCache(new UnlimitedDiscCache(cacheDir))
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging()
				.build();
		ImageLoader.getInstance().init(config);
	}

	/**
	 * 加载图片
	 * 
	 * @param uri
	 * @param imageView
	 */
	public void displayImage(String uri, ImageView imageView)
	{
		this.displayImage(uri, imageView, null, null);
	}

	/**
	 * 加载图片
	 * 
	 * @param uri
	 * @param imageView
	 * @param options
	 */
	public void displayImage(String uri, ImageView imageView,
			DisplayImageOptions options)
	{
		this.displayImage(uri, imageView, options, null);
	}

	/**
	 * 加载图片
	 * 
	 * @param uri
	 * @param imageView
	 * @param listener
	 */
	public void displayImage(String uri, ImageView imageView,
			ImageLoadingListener listener)
	{
		this.displayImage(uri, imageView, null, listener);
	}

	/**
	 * 加载图片
	 * 
	 * @param uri
	 * @param imageView
	 * @param options
	 * @param listener
	 */
	public void displayImage(String uri, ImageView imageView,
			DisplayImageOptions options, ImageLoadingListener listener)
	{
		imageLoader.displayImage(uri, imageView, options, listener);
	}
}
